package hackathon.att.atl.android.autoresponder;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class AutoResponderDAO {

	// Database fields
	  private SQLiteDatabase database;
	  private DatabaseHelper dbHelper;
	  private String[] allColumns = { DatabaseHelper.COLUMN_ID,
			  DatabaseHelper.COL_PHONE_NUM, DatabaseHelper.COL_SELECTED_CONTACT,
			  DatabaseHelper.COL_SEND_LOCATION_FLAG, DatabaseHelper.COL_VOICE_MESSAGE,
			  DatabaseHelper.COL_TEXT_MESSAGE};
	  
	  public AutoResponderDAO(Context context) {
	    dbHelper = new DatabaseHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }
	  
	  public AutoResponderData getContactInfo(long uniqueID)
	  {
		  AutoResponderData contact=null;
		    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,
			        allColumns, DatabaseHelper.COLUMN_ID + " = " + uniqueID, null,
			        null, null, null);
		    
		    if (cursor.getCount() > 0)
		    {
		        cursor.moveToFirst();
		    	contact = cursorToComment(cursor);
		    }
		     
		    return contact;
	  }
	  
	  public AutoResponderData getContactInfoByPhoneNumber(String phNum)
	  {
		  AutoResponderData contact=null;
		    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,
			        allColumns, DatabaseHelper.COL_PHONE_NUM + " = " + phNum, null,
			        null, null, null);
		    
		    // Ensure we have data returned from the query
		    if (cursor.getCount() > 0)
		    {
		    	cursor.moveToLast();
		        contact = cursorToComment(cursor);		    	
		    }

		    return contact;		  
	  }
	  
	  public AutoResponderData updateContactInfo(AutoResponderData data)
	  {
		    ContentValues values = new ContentValues();
//		    values.put(DatabaseHelper.COLUMN_ID, data.getId());
		    values.put(DatabaseHelper.COL_PHONE_NUM, data.getPhoneNumber());
		    values.put(DatabaseHelper.COL_SELECTED_CONTACT, data.getContact_uri());
		    values.put(DatabaseHelper.COL_SEND_LOCATION_FLAG, data.isGetLocationFlag());
		    values.put(DatabaseHelper.COL_TEXT_MESSAGE, data.getText_msg());
		    values.put(DatabaseHelper.COL_VOICE_MESSAGE, data.getVm_uri());
		    
		    long insertId = database.insert(DatabaseHelper.TABLE_NAME, null,
		        values);
		    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,
		        allColumns, DatabaseHelper.COLUMN_ID + " = " + insertId, null,
		        null, null, null);
		    cursor.moveToFirst();
		    AutoResponderData newComment = cursorToComment(cursor);
		    cursor.close();
		    return newComment;  
	  }
	  
	  public void deleteContactInfo(AutoResponderData data)
	  {
		    long id = data.getId();
		    System.out.println("Contact Data deleted with id: " + id);
		    database.delete(DatabaseHelper.TABLE_NAME, DatabaseHelper.COLUMN_ID
		        + " = " + id, null);	  
	  }
	  
	  public List<AutoResponderData> retrieveAll()
	  {
		    List<AutoResponderData> comments = new ArrayList<AutoResponderData>();

		    Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,
		        allColumns, null, null, null, null, null);

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	AutoResponderData comment = cursorToComment(cursor);
		      comments.add(comment);
		      cursor.moveToNext();
		    }
		    // Make sure to close the cursor
		    cursor.close();
		    return comments;  
	  }
	  
	  private AutoResponderData cursorToComment(Cursor cursor) {
		  AutoResponderData comment = new AutoResponderData();
		    comment.setId(cursor.getLong(0));
		    comment.setPhoneNumber(cursor.getString(1));
		    comment.setContact_uri(cursor.getString(2));
		    if (cursor.getInt(3) > 0)
		    {
		    	comment.setGetLocationFlag(true);	
		    }
		    else
		    {
		    	comment.setGetLocationFlag(false);
		    }
		    
		    comment.setVm_uri(cursor.getString(4));
		    comment.setText_msg(cursor.getString(5));
		    return comment;
		  }
}
